package com.ctn.information;

import com.ctn.information.util.SocialUtil;
import com.umeng.socialize.bean.SHARE_MEDIA;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.widget.ImageView;

/**
 * 设置页面
 * @author guilin20120406
 * */
public class SetActivity extends PreferenceActivity implements OnPreferenceChangeListener,OnClickListener{
	// preference key
	private static final String MY_ACCOUNT_KEY = "pref_my_account_key";// 我的账号
	private static final String OTHER_ACCOUNT_KEY="pref_other_account_key";//绑定微博账号
	private static final String CONFIG_UPDATE_OPTION_KEY = "config_update";// 更新版本
	private static final String TEXT_SIZE_KEY="pref_text_size_key";//字体大小设置
	private static final String OTHER_FRESH_GUIDE_KEY="pref_other_fresh_guide_key";//新手引导
	private static final String OTHER_ABOUT_KEY="pref_other_about_key";//关于
	private ImageView backImage;
	ProgressDialog progressDialog;// 更新版本时进度框
	private ListPreference textSizelp;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getPreferenceManager().setSharedPreferencesName("setting");
		addPreferencesFromResource(R.xml.set_preference);
		setContentView(R.layout.set_preference_main);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 backImage=(ImageView)findViewById(R.id.base_action_bar_back);
		 backImage.setOnClickListener(this);
		/* SharedPreferences prefs = PreferenceManager
	                .getDefaultSharedPreferences(this);
	        prefs.registerOnSharedPreferenceChangeListener(this);*/
	     textSizelp = (ListPreference) findPreference(getString(R.string.pref_text_size_key));
	      //  findPreference(getString(R.string.pref_my_account_key)).;
	        textSizelp.setOnPreferenceChangeListener(this);
	       setTextSizePrefernceSummary(textSizelp);
	      SharedPreferences shareSettiong= getSharedPreferences("setting", MODE_PRIVATE);
	      String textSizeValue=shareSettiong.getString(TEXT_SIZE_KEY, "");
	     Log.i("pref_text_size_key", textSizeValue);
	   // webSetting.setTextSize(textSizeValue);
	     
	     
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue)
	{
		Log.i("onPreferenceChange", String.valueOf(newValue));
		String key = preference.getKey();
		if (key.equals(TEXT_SIZE_KEY)) {
			ListPreference textSizeLp=(ListPreference)preference;
			//setTextSizePrefernceSummary( textSizeLp);
			CharSequence[] summaries = getResources().getTextArray(R.array.set_text_size);
			CharSequence[] values=textSizeLp.getEntryValues();
			  for (int i=0; i<values.length; i++)
		        {
		            if (values[i].equals(String.valueOf(newValue)))
		            {
		            	preference.setSummary(summaries[i]);
		                break;
		            }
		        }
			//preference.setSummary(String.valueOf(newValue));					
		}
		
		return true;
	}
	private void setTextSizePrefernceSummary(ListPreference preference){
		CharSequence[] summaries = getResources().getTextArray(R.array.set_text_size);
		CharSequence[] values=preference.getEntryValues();
		String value=preference.getValue();
		 for (int i=0; i<values.length; i++)
	        {
	            if (values[i].equals(value))
	            {
	            	preference.setSummary(summaries[i]);
	            	
	                break;
	            }
	        }
	}
	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference)
	{
		
		String key = preference.getKey();
		Class a =null;

		Log.i("onPreferenceClick", key);
		if (key.equals(MY_ACCOUNT_KEY)) {// 
			//a =MyAccountActivity.class;
			SocialUtil.openSocialCenter(this, SocialUtil.DESCRIPTOR);
		} else if (key.equalsIgnoreCase(OTHER_ABOUT_KEY)) {// 关于
			a =AboutActivity.class;
		}  else if (key.equalsIgnoreCase(OTHER_ACCOUNT_KEY)) {// 绑定新浪微博账号
			//a =BindWeboActivity.class;
			SocialUtil.openOauthVerify(this, SHARE_MEDIA.SINA, SocialUtil.DESCRIPTOR);
		}else if (key.equalsIgnoreCase(OTHER_FRESH_GUIDE_KEY)) {// 新手向导
			a =FreshGuideActivity.class;
		} 
		if(null!=a){
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), a);
			Bundle bundle = new Bundle();
			bundle.putInt("fromActivity", 0);
			intent.putExtras(bundle);
			startActivity(intent);
			return true;
		}
		//return false;
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.base_action_bar_back){
			super.onBackPressed();
		}
	}


}
